﻿//-----------------------------------------------------------------------
// <copyright file="MultipleSelectRefinementPanel.cs" company="Microsoft Services | Global delivery">
// Copyright (c) Microsoft Services | Global delivery.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <date>15-May-2012</date>
// <summary>This class implements the Multiple Select Refinement Panel.</summary>
//----------------------------------------------------------------------- 

namespace Microsoft.Services.SP.WebParts
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml;
    using System.Xml.XPath;

    using Microsoft.Office.Server.Search.WebControls;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    /// <summary>
    /// This class implements the Multiple Select Refinement Panel.
    /// </summary>
    [ToolboxItemAttribute(false)]
    public class MultipleSelectRefinementPanel : RefinementWebPart
    {
        /// <summary>
        /// Visual Studio might automatically update this path when you change the Visual Web Part project item.
        /// </summary>
        private const string AscxPath = @"~/_CONTROLTEMPLATES/Microsoft.Services.SP.WebParts/MultipleSelectRefinementPanel/MultipleSelectRefinementPanelUserControl.ascx";

        /// <summary>
        /// The view path.
        /// </summary>
        private string viewPath = string.Empty;

        /// <summary>
        /// Boolean value to set collapsing for unselected categories.
        /// </summary>
        private bool collapseUnselectedCategories = false;

        /// <summary>
        /// Gets or sets a value indicating whether [collapse unselected categories].
        /// </summary>
        /// <value>
        /// <c>true</c> if [collapse unselected categories]; otherwise, <c>false</c>.
        /// </value>
        [WebBrowsable(true), Category("Miscellaneous"), DefaultValue(false),
        FriendlyName("Collapse Unselected Categories"), Personalizable(PersonalizationScope.Shared),
        Description("Collapse unselected categories in the refinement panel."), WebPartStorage(Storage.Shared)]
        public bool CollapseUnselectedCategories
        {
            get
            {
                return this.collapseUnselectedCategories;
            }

            set 
            { 
                this.collapseUnselectedCategories = value;
            }
        }

        /// <summary>
        /// Raises the <see cref="E:Init"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            if (this.UseDefaultConfiguration)
            {
                this.NumberOfCharsToDisplay = 17;
            }

            base.OnInit(e);
        }

        /// <summary>
        /// Creates the child controls.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            CssRegistration.Register("/_layouts/MultipleSelectRefinementPanel/styles.css");

            if (!this.Page.ClientScript.IsClientScriptIncludeRegistered("jquery"))
            {
                string js = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Microsoft.Services.SP.WebParts.MultipleSelectRefinementPanel.js.jquery.min.js");
                this.Page.ClientScript.RegisterClientScriptInclude("jquery", js);
            }

            ////Control control = Page.LoadControl(_ascxPath);
            MultipleSelectRefinementPanelUserControl control = (MultipleSelectRefinementPanelUserControl)Page.LoadControl(AscxPath);
            control.RefinerXml = this.viewPath;
            control.FilterCategoryDefinition = this.FilterCategoriesDefinition;
            control.CollapseUnselectedCategories = this.CollapseUnselectedCategories;
            Controls.Add(control);
        }

        /// <summary>
        /// Gets the XPath navigator.
        /// </summary>
        /// <param name="viewPath">The view path.</param>
        /// <returns>The XPath Navigator.</returns>
        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            XPathNavigator nav = base.GetXPathNavigator(viewPath);

            if (nav == null)
            {
                return nav;
            }

            this.viewPath = nav.InnerXml;
            XPathNodeIterator categories = nav.SelectSingleNode("/FilterPanel").SelectChildren(XPathNodeType.Element);
            string category = String.Empty;
            List<string> deleteCategories = new List<string>();
            while (categories.MoveNext())
            {
                category = categories.Current.GetAttribute("DisplayName", string.Empty);
                deleteCategories.Add(category);
            }

            foreach (string delCategory in deleteCategories)
            {
                nav.SelectSingleNode("/FilterPanel/FilterCategory[@DisplayName=\"" + delCategory + "\"]").DeleteSelf();
            }

            return nav;
        }
    }
}
